<?php
require_once ROOT_PATH . '/library/Soooner/Controller/Action.php';
class RoleController extends Soooner_Controller_Action
{
	private $_roleid;
	private $_modelRole;
    public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();
    	$this->_modelRole = new Model_Tables_Role();
    }
    /**
    * 获取角色列表
    * @return unknown_type
    */
    public function indexAction()
    {
    	$options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $ItemCount =  $this->_modelRole->getCount();
        $PageCount = ceil($ItemCount/$numPerPage);
        $PageCount = $PageCount ? $PageCount : 1;
        $template = new Soooner_View_Template();
		include $this->_dzTemplate->getFile( 'role.manage.html' );
    }
    /**
    * 获取角色列表
    * @return unknown_type
    */
    public function indexajaxAction()
    {
        $options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $page = intval($this->_getParam('page'));//默认页码
        $page = empty($page)?'1':$page;
   		$s = $this->_getParam('s'); 
        $ItemCount =  $this->_modelRole->getCount($s);
        $PageCount = ceil($ItemCount/$numPerPage);
        if($page>$PageCount)$page=$PageCount;
        $infos = $this->_modelRole->getList( $page, $numPerPage, $s );
        include $this->_dzTemplate->getFile( 'ajax/role.manage.ajax.html' );
    }
    /**
    * 添加角色
    * @return unknown_type
    */
    public function addAction()
    {
    	$request = $this->getRequest();        
        if ($request->isPost())
        {
            $params =$request->getPost();
            $this->_modelRole->add($params);                
            $this->_redirect("/role/index/");
        } 
        include $this->_dzTemplate->getFile( 'role.add.html' );
    }
    /**
    * 修改角色
    * @return unknown_type
    */
    public function modifyAction()
    {
    	$roleid = $this->_getParam('roleid');
    	$info = $this->_modelRole->getInfo($roleid); 
        $request = $this->getRequest();        
        if ($request->isPost())
        {
        	$request =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
                $model->modify($request,$roleid);
                $this->_redirect("/role/");
            }
        } 
        include $this->_dzTemplate->getFile( 'role.edit.html' );
    }
    /**
    * 无刷新修改角色
    * @return unknown_type
    */    
    public function modifyajaxAction()
    {
        $request = $this->getRequest();
        $params = $request->getPost();
        $arr = split("\|", $params['id']);
        $field = $arr[0];
        $roleid = $arr[1];
        $value = stripslashes($params['value']);
        $this->_modelRole->modifyAjax($roleid,$field,$value);
        echo $value;
        exit;
    }
    public function modifyajaxreversAction()
    {
    	$field = $this->_getParam('field');
    	$pri = $this->_getParam('pri');
        $this->_modelRole->modifyAjaxRevers($field,$pri);
        exit;
    }
    
    /**
    * 删除角色
    */
    public function deleteAction()
    {
        $roleid = $this->_getParam('roleid');   
        $this->_modelRole->del($roleid); 
        $this->_redirect("/role/");
    }
    /**
     * 设置角色权限
     */
    public function modifyprivAction()
    {
    	$template = new Soooner_View_Template();
    	$resources = $this->_modelRole->getResourceList();
    	//$processes = $this->_modelRole->getWorkFlowProcessList();
    	$roleid = $this->_getParam('roleid');
        $request = $this->getRequest();  
        $valueresource = $this->_modelRole->getRolePriv($roleid,"resource");
        //$valueprocess = $this->_modelRole->getRolePriv($roleid,"process");        
        if ($request->isPost())
        {
        	$request =$request->getPost(); 
            if (true)//服务端检查输入参数
            {
            	$resource = $request['resource'];
            	$process = $request['process'];
            	print_r($process);
            	$this->_modelRole->modifyPriv($roleid,$resource,$process); 
                //$model->modify($request,$roleid);
                $this->_redirect("/role");
            }
        } 
        //exit();
        include $this->_dzTemplate->getFile( 'role.priv.edit.html' );
    }
}

